<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <meta http-equiv="Pragma" content="no-cache" />
    <title>付款</title>
    <link rel="stylesheet" href="css/weui.min.css" />
    <link rel="stylesheet" href="fonts/iconfont.css" />
    <link rel="stylesheet" href="css/style.css" />
    <link rel="stylesheet" href="css/payment.css" />
    <script src="js/jquery-2.2.3.min.js"></script>
    <!-- <script src="js/weui.min.js"></script> -->

</head>

<body ontouchstart>

    <div class="weui-content">
        <div class="payment-title">请输入金额</div>
        <div class="input-wrapper">
            <input class="payment-box" type="text" id="price">
        </div>
        <div id="purse-balance"> 钱包余额</div>
        <div id="welfare-cash">
            <span>福利金:
                <span id="welfare">0</span>
                元
            </span>
            <span style="float:right;">
                现金:
                <span id="cash"> 0</span>
                元
            </span>
        </div>
        <div style="margin:auto;width:354px;">
            <button id="submit">
                确认支付
            </button>
        </div>
    </div>
</body>
<script src="./api.js"></script>
<script>
    window.onload = function () {
        let api = getApi()
        if (localStorage.getItem('wxUserInfo')) {
            let userinfo = JSON.parse(localStorage.getItem('wxUserInfo')).data
            let customerID = userinfo.customer.customerID
            customerID = Number(customerID)

            //获取本地缓存token
            let token = userinfo.token

            //获取福利金 现金
            function getAccountAmount() {
                fetch(api + '/customer/' + customerID, {
                    method: "get",
                    headers: {
                        'Content-type': 'application/json',
                        "Authorization": token
                    }
                })
                    .then(res => res.json())
                    .then((data) => {
                        // 福利金
                        let welfare = data.data.welfare
                        //现金
                        let cash = data.data.cash
                        //总余额
                        let totalBalance = data.data.welfare + data.data.cash

                        $('#cash').html(cash)
                        $('#welfare').html(welfare)

                    }).catch((err) => {
                        window.location.href = './index.html'
                    })
            }

            //调用获取福利金
            getAccountAmount()

            // 获取url及参数
            let windowIDstr = window.location.href.split('?')[1]
            if (windowIDstr.indexOf('price') != -1) {

                // ==========输入框不可以编辑=============
                let paramsArry = windowIDstr.split('&')
                let windowID = paramsArry[0].split('=')[1]
                windowID = Number(windowID)
                let price = paramsArry[1].split('=')[1]
                price = Number(price)
                let orderID = paramsArry[2].split('=')[1]
                let note = paramsArry[3].split('=')[1]
                $('#price').val(price)
                $('#price').attr('disabled', true)

                //点击确认支付
                $('#submit').on('click', function () {
                    let data = {
                        customerID: customerID,
                        windowID: windowID,
                        price: price,
                        orderID: orderID,
                        note: note
                    }

                    //发起支付请求
                    fetch(`${api}/consumingRecord/scan`, {
                        method: "POST",
                        headers: {
                            'Content-type': 'application/json'
                        },
                        body: JSON.stringify(data)
                    })
                        .then(res => res.json())
                        .then((data) => {
                            if (data.data.state == true) {
                                alert('支付成功')
                                window.location.href = './zhanghu.html'
                            }else{
                                alert('支付失败')
                            }
                        }).catch((err) => {
                            alert('支付失败')
                        })
                })
            } else {
                //=============输入框可以编辑=============
                //点击确认支付
                $('#submit').on('click', function () {
                    let windowID = windowIDstr.split('=')[1]
                    windowID = Number(windowID)
                    let price = $('#price').val()
                    price = Number(price)
                    let data = {
                        customerID: customerID,
                        windowID: windowID,
                        price: price,
                        orderID: '',
                        note: ''
                    }
                    
                    //发起支付请求
                    fetch(`${api}/consumingRecord/scan`, {
                        method: "POST",
                        headers: {
                            'Content-type': 'application/json'
                        },
                        body: JSON.stringify(data)
                    })
                        .then(res => res.json())
                        .then((data) => {
                            if (data.data.state == true) {
                                alert('支付成功')
                                window.location.href = './zhanghu.html'
                            }else{
                                alert('支付失败')
                            }
                        }).catch((err) => {
                            alert(err)
                        })
                })
            }
        } else {
            alert('请先登录')
            window.location.href = './index.html'
        }
    }
</script>

</html>